<template>
    <Datalist :name="name" ref="datalist" :excelHeader="excelHeader" skipExcelNumber="3" :cate="cate" :cols="cols" />
</template>
<script>
import Datalist from "@/components/DataList";
import { getUser } from "@/utils/auth";
import {get教师基本情况,get教师基本情况ByGH} from "@/api/教师基本情况"
import linq from "linq"
import { addCyjx, deleteCyjx, updateCyjx, getCyjx,bulkAddCYJX } from "@/api/参与教学联系学生.js";
import { unzipSync } from "zlib";
export default {
    components: {
        Datalist
    },
    data() {
        return {
            cate: "A2-2参与教学、联系学生",
              name:'2.2 参与教学联系学生',
            excelHeader:{B3:"工号",C3:"姓名",D3:"分管工作",E3:"关注教学和学生情况",E4:"听课（节）",F4:"走访学生寝室（次）",G4:"走访校外实习点（次）",H4:"参与学生社团文体活动（次）"},
            cols: [
                { label: "教工号", prop: "GH"},
                { label: "姓名", prop: "XM"},
                { label: "分管工作", prop: "ZGHCSDGZ"},
                {
                    label: "关注教学和学生情况",
                    data: [
                        { label: "听课(节)", prop: "GZXS_TKCS"},
                        { label: "走访学生寝室(次)", prop: "GZXS_SXCS"},
                        { label: "走访校外实习点(次)", prop: "GZXS_SXD"},
                        { label: "参与学生社团文体活动(次)", prop: "GZXS_WTHD"},
                    ]
                }
            ]
        }
        },
        methods:{
              getSummaries(list){
      //
       
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
         sums[0]="合计";

        sums[7]= "人数:"+linq.from(list).distinct("$.GH").count();
        sums[9]= "听课:"+linq.from(list).sum("$.GZXS_TKCS");
        sums[10]= "走访学生寝室:"+linq.from(list).sum("$.GZXS_SXCS");
        sums[11]= "走访校外实习点:"+linq.from(list).sum("$.GZXS_SXD");
        sums[12]= "参与学生社团文体活动:"+linq.from(list).sum("$.GZXS_WTHD");
        sums[13]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
        sums[13] += ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
        sums[13] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
       return sums;
      //
    },
     async onDlgCreated(model) {
            var dd = findComponent(this,"GH");
            var  res = await get教师基本情况(user.XXID,user.CJPC);
            dd.$attrs.items = res.map(item => {
                return { label: item.GH, value: item.GH};
      });
      
    },
    
    async fetchData() {
      var user = getUser();
      var datas = await getCyjx(user.XXID,user.CJPC);    
      return datas;
    },
     add_check(model,list){
      //
          //是否已经存在数据
          var first =  linq.from(list).firstOrDefault(e=>e.GH ==  model.GH);
          console.log(first)
          if(first)
          return "已经存在对应教工号教师"
      //
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
      try{
        
        console.log(JSON.stringify(temp));
        var model = await addCyjx(temp);
        resolve(model);
      }catch(e){
        reject(e);
      }
       
      });
    },
    
    // onadd(model) {
    //   //
    //   var temp = JSON.parse(JSON.stringify(model))
    //   return new Promise(async (resolve,reject)=>{
           
      
    //       console.log(JSON.stringify(temp));
    //       var model = await addCyjx(temp);
    //       resolve(model);
    //   })
      
    // },
      async  bulkAdd(array){
       await  bulkAddCYJX({CYJX:array});
    },
     check(array,list) {
    return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
          var 教工号 = await get教师基本情况(user.XXID,user.CJPC)
         
    
          var duplicates =  linq.from(array).groupBy("$.GH").where("$.count()>1").toArray();
          duplicates.forEach(e=>{
            linq.from(array).where(t=>t.GH==e.key()).forEach(t=>t.error="录入数据存在相同的教工号");
          })
          //是否已经存在数据
        
          var err =  linq.from(array).select("$.GH").intersect(linq.from(list).select("$.GH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.GH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中已经存在教工号");
          
         //是否已经存在相同
          
          err =  linq.from(array).select("$.GH").except(linq.from(教工号).select("$.GH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.GH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"教师基本情况表不存在对应的教工号");
          //把错误的教师姓名替代掉
          err =  linq.from(array).select("$.GH+','+$.XM").except(linq.from(教工号).select("$.GH+','+$.XM").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.GH+','+e.XM) !==-1).
          forEach(e=>{
            var d = linq.from(教工号).firstOrDefault(item=>item.GH == e.GH);
            if(d){
              e.XM = d.XM;
            }
          });
            resolve();
          }catch(e){
            reject(e);
          }
     })
   
    },
     async  onChange(model, field, value) {
 
      //
         if(field == "GH"){
         if(value == "error"){
           console.log("error")
          model.GH=""
          model.XM= "";
      
     
         }else{
         var d1 =  await get教师基本情况ByGH(this.$root.user.XXID,this.$root.user.CJPC,value);
        
           console.log("udpate ")
          model.GH=d1[0].GH;
          model.XM= d1[0].XM;    
         }
       }
      //
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await updateCyjx(temp);
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
        
         await deleteCyjx(temp);
          resolve();
      })
    }
        }
}
</script>
<style>
</style>

